package com.xxl.controller;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * jdbc测试
 * @author xxl
 * @date 2023/3/11
 */
@RestController
public class JDBCController {
    JdbcTemplate jdbcTemplate;
    @Resource
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        //JdbcTemplate来自于DataSource
        this.jdbcTemplate = jdbcTemplate;
    }
    @GetMapping("insert")
    public String insert() {
        String sql = "insert into jdbc_demo01.demo08(name,tid) value (?,?)";
        int update = jdbcTemplate.update(sql, new Object[]{"tyl", 1});
        System.out.println(update);
        return ""+update;
    }

    @GetMapping("update")
    public String update() {
        String sql = "update jdbc_demo01.demo08 set name = ? where id = ? ";
        int update = jdbcTemplate.update(sql, new Object[]{"wyx", 2});
        System.out.println(update);
        return ""+update;
    }

    @GetMapping("delete")
    public String delete() {
        String sql = "delete from jdbc_demo01.demo08 where id = ?";
        int update = jdbcTemplate.update(sql, new Object[]{1});
        System.out.println(update);
        return ""+update;
    }

    @GetMapping("query")
    public String query() {
        String sql = "select * from jdbc_demo01.demo08";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
        System.out.println(maps);
        return maps.toString();
    }
}
